| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343 |
10x
10x
10x
10x
10x
10x
10x
10x
10x
10x
10x
10x
10x
10x
10x
5x
5x
5x
5x
5x
5x
10x
43x
37x
8x
35x
30x
8x
27x
20x
4x
23x
16x
4x
19x
12x
4x
15x
8x
4x
11x
11x
2x
9x
5x
5x
5x
5x
5x
5x
5x
5x
5x
5x
10x
8x
2x
6x
2x
2x
2x
2x
2x
2x
2x
2x
2x
40x
40x
40x
40x
40x
40x
40x
40x
2x
38x
2x
36x
2x
34x
2x
32x
2x
30x
2x
28x
4x
4x
4x
4x
4x
4x
4x
4x
4x
10x
5x
3x
2x
2x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
10x
| /**
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
import DBInterface from './db-interface';
import Errors from './errors';
import arrayBufferToBase64 from '../helpers/array-buffer-to-base64';
import { cleanV1 } from './clean-v1-undefined';
const FCM_TOKEN_OBJ_STORE = 'fcm_token_object_Store';
const DB_NAME = 'fcm_token_details_db';
const DB_VERSION = 2;
/** @record */
function ValidateInput() {}
/** @type {string|undefined} */
ValidateInput.prototype.fcmToken;
/** @type {string|undefined} */
ValidateInput.prototype.swScope;
/** @type {string|undefined} */
ValidateInput.prototype.vapidKey;
/** @type {PushSubscription|undefined} */
ValidateInput.prototype.subscription;
/** @type {string|undefined} */
ValidateInput.prototype.fcmSenderId;
/** @type {string|undefined} */
ValidateInput.prototype.fcmPushSet;
export default class TokenDetailsModel extends DBInterface {
constructor() {
super(DB_NAME, DB_VERSION);
}
onDBUpgrade(db: IDBDatabase, evt: IDBVersionChangeEvent) {
Eif (evt.oldVersion < 1) {
// New IDB instance
var objectStore = db.createObjectStore(FCM_TOKEN_OBJ_STORE, {
keyPath: 'swScope'
});
// Make sure the sender ID can be searched
objectStore.createIndex('fcmSenderId', 'fcmSenderId', {
unique: false
});
objectStore.createIndex('fcmToken', 'fcmToken', {
unique: true
});
}
Eif (evt.oldVersion < 2) {
// Prior to version 2, we were using either 'fcm_token_details_db'
// or 'undefined' as the database name due to bug in the SDK
// So remove the old tokens and databases.
cleanV1();
}
}
/**
* This method takes an object and will check for known arguments and
* validate the input.
* @private
* @param {!ValidateInput} input
* @return {!Promise} Returns promise that resolves if input is valid,
* rejects otherwise.
*/
async validateInputs_(input) {
if (input.fcmToken) {
if (typeof input.fcmToken !== 'string' || input.fcmToken.length === 0) {
return Promise.reject(
this.errorFactory_.create(Errors.codes.BAD_TOKEN)
);
}
}
if (input.swScope) {
if (typeof input.swScope !== 'string' || input.swScope.length === 0) {
return Promise.reject(
this.errorFactory_.create(Errors.codes.BAD_SCOPE)
);
}
}
if (input.vapidKey) {
if (
!(input.vapidKey instanceof Uint8Array) ||
input.vapidKey.length !== 65
) {
return Promise.reject(
this.errorFactory_.create(Errors.codes.BAD_VAPID_KEY)
);
}
}
if (input.subscription) {
if (!(input.subscription instanceof PushSubscription)) {
return Promise.reject(
this.errorFactory_.create(Errors.codes.BAD_SUBSCRIPTION)
);
}
}
if (input.fcmSenderId) {
if (
typeof input.fcmSenderId !== 'string' ||
input.fcmSenderId.length === 0
) {
return Promise.reject(
this.errorFactory_.create(Errors.codes.BAD_SENDER_ID)
);
}
}
if (input.fcmPushSet) {
if (
typeof input.fcmPushSet !== 'string' ||
input.fcmPushSet.length === 0
) {
return Promise.reject(
this.errorFactory_.create(Errors.codes.BAD_PUSH_SET)
);
}
}
}
/**
* Given a token, this method will look up the details in indexedDB.
* @param {string} fcmToken
* @return {Promise<Object>} The details associated with that token.
*/
getTokenDetailsFromToken(fcmToken) {
if (!fcmToken) {
return Promise.reject(this.errorFactory_.create(Errors.codes.BAD_TOKEN));
}
return this.validateInputs_({ fcmToken })
.then(() => {
return this.openDatabase();
})
.then(db => {
return new Promise((resolve, reject) => {
const transaction = db.transaction([FCM_TOKEN_OBJ_STORE]);
const objectStore = transaction.objectStore(FCM_TOKEN_OBJ_STORE);
const index = objectStore.index('fcmToken');
const request = index.get(fcmToken);
request.onerror = function(event) {
reject((<IDBRequest>event.target).error);
};
request.onsuccess = function(event) {
const result = (<IDBRequest>event.target).result
? (<IDBRequest>event.target).result
: null;
resolve(result);
};
});
});
}
/**
* Given a service worker scope, this method will look up the details in
* indexedDB.
* @public
* @param {string} swScope
* @return {Promise<Object>} The details associated with that token.
*/
getTokenDetailsFromSWScope(swScope) {
if (!swScope) {
return Promise.reject(this.errorFactory_.create(Errors.codes.BAD_SCOPE));
}
return this.validateInputs_({ swScope })
.then(() => {
return this.openDatabase();
})
.then(db => {
return new Promise((resolve, reject) => {
const transaction = db.transaction([FCM_TOKEN_OBJ_STORE]);
const objectStore = transaction.objectStore(FCM_TOKEN_OBJ_STORE);
const scopeRequest = objectStore.get(swScope);
scopeRequest.onerror = event => {
reject((<IDBRequest>event.target).error);
};
scopeRequest.onsuccess = event => {
const result = (<IDBRequest>event.target).result
? (<IDBRequest>event.target).result
: null;
resolve(result);
};
});
});
}
/**
* Save the details for the fcm token for re-use at a later date.
* @param {{swScope: !string, vapidKey: !string,
* subscription: !PushSubscription, fcmSenderId: !string, fcmToken: !string,
* fcmPushSet: !string}} input A plain js object containing args to save.
* @return {Promise<void>}
*/
saveTokenDetails({
swScope,
vapidKey,
subscription,
fcmSenderId,
fcmToken,
fcmPushSet
}) {
if (!swScope) {
return Promise.reject(this.errorFactory_.create(Errors.codes.BAD_SCOPE));
}
if (!vapidKey) {
return Promise.reject(
this.errorFactory_.create(Errors.codes.BAD_VAPID_KEY)
);
}
if (!subscription) {
return Promise.reject(
this.errorFactory_.create(Errors.codes.BAD_SUBSCRIPTION)
);
}
if (!fcmSenderId) {
return Promise.reject(
this.errorFactory_.create(Errors.codes.BAD_SENDER_ID)
);
}
if (!fcmToken) {
return Promise.reject(this.errorFactory_.create(Errors.codes.BAD_TOKEN));
}
if (!fcmPushSet) {
return Promise.reject(
this.errorFactory_.create(Errors.codes.BAD_PUSH_SET)
);
}
return this.validateInputs_({
swScope,
vapidKey,
subscription,
fcmSenderId,
fcmToken,
fcmPushSet
})
.then(() => {
return this.openDatabase();
})
.then(db => {
/**
* @dict
*/
const details = {
swScope: swScope,
vapidKey: arrayBufferToBase64(vapidKey),
endpoint: subscription.endpoint,
auth: arrayBufferToBase64(subscription['getKey']('auth')),
p256dh: arrayBufferToBase64(subscription['getKey']('p256dh')),
fcmSenderId: fcmSenderId,
fcmToken: fcmToken,
fcmPushSet: fcmPushSet,
createTime: Date.now()
};
return new Promise((resolve, reject) => {
const transaction = db.transaction(
[FCM_TOKEN_OBJ_STORE],
this.TRANSACTION_READ_WRITE
);
const objectStore = transaction.objectStore(FCM_TOKEN_OBJ_STORE);
const request = objectStore.put(details);
request.onerror = event => {
reject((<IDBRequest>event.target).error);
};
request.onsuccess = event => {
resolve();
};
});
});
}
/**
* This method deletes details of the current FCM token.
* It's returning a promise in case we need to move to an async
* method for deleting at a later date.
* @return {Promise<Object>} Resolves once the FCM token details have been
* deleted and returns the deleted details.
*/
deleteToken(token: string) {
if (typeof token !== 'string' || token.length === 0) {
return Promise.reject(
this.errorFactory_.create(Errors.codes.INVALID_DELETE_TOKEN)
);
}
return this.getTokenDetailsFromToken(token).then(details => {
if (!details) {
throw this.errorFactory_.create(Errors.codes.DELETE_TOKEN_NOT_FOUND);
}
return this.openDatabase().then(db => {
return new Promise((resolve, reject) => {
const transaction = db.transaction(
[FCM_TOKEN_OBJ_STORE],
this.TRANSACTION_READ_WRITE
);
const objectStore = transaction.objectStore(FCM_TOKEN_OBJ_STORE);
const request = objectStore.delete(details['swScope']);
request.onerror = event => {
reject((<IDBRequest>event.target).error);
};
request.onsuccess = event => {
Iif ((<IDBRequest>event.target).result === 0) {
reject(
this.errorFactory_.create(Errors.codes.FAILED_TO_DELETE_TOKEN)
);
return;
}
resolve(details);
};
});
});
});
}
}
|